<?php

require FCPATH . 'application/controllers/backend/backend.php';

class Project extends Backend {

    public function __construct() {
	parent::__construct();

	$this->load->model('project_model', 'model');
    }

    public function index() {
	$this->load->model('label_model');

	$data = array(
	    'types' => $this->label_model->records(0, 0, array(array('parent', 1))),
	    'areas' => $this->label_model->records(0, 0, array(array('parent', 2)))
	);

	$this->load_javascripts('backend_project_index');

	$this->rend('project_index_view', $data);
    }

    public function add() {
	$this->load->model('label_model');

	$data = array(
	    'types' => $this->label_model->records(0, 0, array(array('parent', 1))),
	    'areas' => $this->label_model->records(0, 0, array(array('parent', 2)))
	);

	$this->load_javascripts(array(
	    'uploadify/swfobject',
	    'uploadify/jquery.uploadify',
	    'backend_project_form'
	));

	$this->rend('project_add_view', $data);
    }

    public function insert() {
	$data = array(
	    'album' => intval($this->input->post('album')),
	    'type' => intval($this->input->post('type')),
	    'year' => intval($this->input->post('year')),
	    'area' => intval($this->input->post('area')),
	    'title' => trim($this->input->post('title')),
	    'title_en' => trim($this->input->post('title_en')),
	    'content' => trim($this->input->post('content')),
	    'content_en' => trim($this->input->post('content_en')),
	    'display' => intval($this->input->post('display')),
	    'created_at' => date('Y-m-d', time())
	);
	
	$max_order = $this->model->max_order();
	$data['order'] = intval($max_order) + 1;

	if (empty($data['title'])) {
	    $this->json_back('XT002', '中文标题不能为空');
	}
	if (empty($data['content'])) {
	    $this->json_back('XT002', '中文内容不能为空');
	}
	if (empty($data['title_en'])) {
	    $this->json_back('XT002', '英文标题不能为空');
	}
	if (empty($data['content_en'])) {
	    $this->json_back('XT002', '英文内容不能为空');
	}

	if (($id = $this->model->insert($data)) > 0) {
	    $this->json_back('XT001', '新增成功', array('id' => $id,'order'=>$data['order']));
	} else {
	    $this->json_back('XT002', '数据库错误');
	}
    }

    public function edit() {
	$id = intval($this->uri->segment(4));

	$this->load_javascripts(array(
	    'uploadify/swfobject',
	    'uploadify/jquery.uploadify',
	    'backend_project_form'
	));

	$this->load->model('label_model');

	$data = array(
	    'project' => $this->model->find($id),
	    'types' => $this->label_model->records(0, 0, array(array('parent', 1))),
	    'areas' => $this->label_model->records(0, 0, array(array('parent', 2)))
	);

	if ($data['project']->album > 0) {
	    $this->load->model('photo_model');
	    $data['photos'] = $this->photo_model->records(0, 0, array(array('album', $data['project']->album)));
	}

	$this->rend('project_edit_view', $data);
    }

    public function update() {
	$id = intval($this->input->post('id'));

	$data = array(
	    'album' => intval($this->input->post('album')),
	    'type' => intval($this->input->post('type')),
	    'year' => intval($this->input->post('year')),
	    'area' => intval($this->input->post('area')),
	    'title' => trim($this->input->post('title')),
	    'title_en' => trim($this->input->post('title_en')),
	    'content' => trim($this->input->post('content')),
	    'content_en' => trim($this->input->post('content_en')),
	    'display' => intval($this->input->post('display'))
	);

	if (empty($data['title'])) {
	    $this->json_back('XT002', '中文标题不能为空');
	}
	if (empty($data['content'])) {
	    $this->json_back('XT002', '中文内容不能为空');
	}
	if (empty($data['title_en'])) {
	    $this->json_back('XT002', '英文标题不能为空');
	}
	if (empty($data['content_en'])) {
	    $this->json_back('XT002', '英文内容不能为空');
	}

	if ($this->model->update($id, $data)) {
	    $this->json_back('XT001', '修改成功', array('id' => $id));
	} else {
	    $this->json_back('XT002', '数据库错误');
	}
    }

    public function show() {
	$id = intval($this->uri->segment(4));

	$data = array(
	    'project' => $this->model->find($id)
	);

	if ($data['project']->album > 0) {
	    $this->load->model('photo_model');
	    $data['photos'] = $this->photo_model->records(0, 0, array(array('album', $data['project']->album)));
	}

	$this->rend('project_show_view', $data);
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));

	$page = $page <= 0 ? 1 : $page;
	$page_size = $page_size <= 0 ? 20 : $page_size;

	$offset = $page_size * ($page - 1);

	$type = intval($this->input->get('type'));
	$year = intval($this->input->get('year'));
	$area = intval($this->input->get('area'));
	$title = trim($this->input->get('title'));

	$conditions = array();

	if ($type) {
	    $conditions[] = array('t.id', $type);
	}

	if ($year) {
	    $conditions[] = array('year', $year);
	}

	if ($area) {
	    $conditions[] = array('a.id', $area);
	}

	if (!empty($title)) {
	    $conditions[] = array('title like', "%{$title}%");
	}

	$this->json_back('XT001', '', array(
	    'total' => $this->model->total($conditions),
	    'records' => $this->model->records($page_size, $offset, $conditions)
	));
    }

    public function delete() {
	$id = intval($this->uri->segment(4));
	
	$project = $this->model->find($id);
	
	if(intval($project->album) > 0){
	    $this->delete_photos($project->album);
	}
	
	if ($this->model->delete($id)) {
	    $this->json_back('XT001', '删除成功', array('id' => $id));
	} else {
	    $this->json_back('XT002', '数据库错误');
	}
    }

    public function order() {
	$id = intval($this->input->post('id'));
	$order = intval($this->input->post('order'));

	$max_order = $this->model->max_order();

	if ($order > ($max_order + 1)) {
	    //$this->json_back('XT002',"请输入1到{$max_order}之间的一个数。");
	}

	if ($this->model->order($id, $order)) {
	    $this->json_back('XT001', '排序成功');
	} else {
	    $this->json_back('XT002', '数据库错误');
	}
    }
    
    private function delete_photos($album){
	$this->load->model('photo_model');
	$this->load->model('album_model');
	$conditions = array(array('album',$album));
	$photos = $this->photo_model->records(0,0,$conditions);
	
	$this->album_model->delete($album);
	
	$this->photo_model->delete($conditions);
	
	if(isset($photos[0])){
	    foreach($photos as $photo){
		$path = FCPATH.'/assets/uploads/'.$photo->file_name;
		if(file_exists($path)){
		    @unlink($path);
		}
	    }
	}
    }

}